package Structure1.mystack.impl;

import Structure1.mystack.MyStack;

import java.lang.reflect.Array;

/**
 * Created by Administrator on 2017/3/16.
 */
public class MyStackImpl<E> implements MyStack<E> {
    private int maxsize;
    private E[] data;
    private int top;
//    初始化
    @SuppressWarnings("unchecked")
    public MyStackImpl(Class<E> type,int size) {
        data = (E[]) Array.newInstance(type,size);
    }
//    入栈
    @Override
    public E push(E item) {
        if (!isFull()) {
            data[++top] = item;
            return item;
        } else {
            return null;
        }
    }
//    出栈
    @Override
    public E pop() {
        E item = null;
        if (!isEmpty()) {
            item = data[top--];
        }
        return item;
    }
//    获取栈元素
    @Override
    public E peak() {
        E item = null;
        if (!isEmpty()) {
            item = data[top];
        }
        return item;
    }
//    判断栈空间是否为满
    @Override
    public boolean isEmpty() {
        if (top == -1) {
            return true;
        } else {
            return false;
        }
    }
//    判断栈是否为空
    @Override
    public boolean isFull() {
        if (top == maxsize-1) {
            return true;
        }
        return false;
    }

    @Override
    public int getSize() {
        return top+1;
    }
}
